﻿///
/// Authors: Lee Gary 
///
/// Date: 24/09/2010
///
/// Purpose: 
///
/// Base class for easy access to log4net
/// 
/// Future:
/// 1.) ReadResourceValue to retrieve exception code from resources file instead of hardcoding
/// 
using System;
using System.Reflection;
using System.Resources;
using System.Text;
using log4net;

namespace GSPMigratorCore.Misc
{
    public class BaseClass
    {
        protected BaseClass()
        {
            if (log4net.LogManager.GetRepository().Configured == false)
            {
                log4net.Config.XmlConfigurator.Configure();
            }
        }
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private static StringBuilder sb;
        private static string NEWLINE = System.Environment.NewLine;
        private static string loggerName;

        /// <summary>
        /// Enum for log levels
        /// </summary>
        protected enum LogLevel
        {
            Info,
            Debug,
            Error
        };

        /// <summary>
        /// Easy way to implement different log levels, removed stringbuilder instance due
        /// to change of logging requirements to exclude timelogs, log levels and thread
        /// </summary>
        /// <param name="loglevel"></param>
        /// <param name="msg"></param>
        protected static void WriteLog(LogLevel loglevel, string msg)
        {
            #region switch loglevel
            switch (loglevel)
            {
                case LogLevel.Debug:
                    {
                        log.Debug(msg);
                        break;
                    }
                case LogLevel.Error:
                    {
                        log.Error(msg);
                        break;
                    }
                case LogLevel.Info:
                    {
                        log.Info(msg);
                        break;
                    }
            }
            #endregion
        }

        /// <summary>
        /// Dreaded exception log which hopefully i do not have to use
        /// </summary>
        /// <param name="ex"></param>
        protected static void ExceptionLog(Exception ex)
        {
            loggerName = MethodBase.GetCurrentMethod().DeclaringType.ToString();
            ILog logEngine = LogManager.GetLogger(loggerName);
            sb = new StringBuilder();

            if (logEngine.IsFatalEnabled && !ex.Message.Contains("Thread was being aborted"))
            {
                sb.Append(NEWLINE);
                sb.Append(ex.Message);
                sb.Append(NEWLINE);
                sb.Append(ex.StackTrace);
                sb.Append(NEWLINE);
                logEngine.Error(sb.ToString());
            }
        }

        [Obsolete]
        public string ReadResourceValue(string key)
        {
            //value for our return value
            string resourceValue = string.Empty;
            try
            {
                string resourceFile = "Resource1";
                string filePath = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
                ResourceManager resourceManager = ResourceManager.CreateFileBasedResourceManager(resourceFile, filePath, null);
                resourceValue = resourceManager.GetString(key);
            }
            catch (Exception ex)
            {
                ExceptionLog(ex);
                resourceValue = string.Empty;
            }
            return resourceValue;
        }
    }
}
